using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;

public class UIGameStart : UIBase
{
    private int currentLevelIndex = 0;
    
    private Button leftButton;
    private Button rightButton;
    private Button newGameButton;
    private Button resumeButton;
    
    private CanvasGroup leftButtonCanvasGroup;
    private CanvasGroup rightButtonCanvasGroup;
    private CanvasGroup resumeButtonCanvasGroup;
    
    public RectTransform levelSwitchMask;
    

    protected override void Awake()
    {
        base.Awake();
        
        uiName = "GameStart";
        leftButton = transform.Find("left").GetComponent<Button>();
        rightButton = transform.Find("right").GetComponent<Button>();
        newGameButton = transform.Find("NewGamebtn").GetComponent<Button>();
        resumeButton = transform.Find("Resumebtn").GetComponent<Button>();
        leftButtonCanvasGroup = leftButton.GetComponent<CanvasGroup>();
        rightButtonCanvasGroup = rightButton.GetComponent<CanvasGroup>();
        resumeButtonCanvasGroup = resumeButton.GetComponent<CanvasGroup>();
    }


    private void Start()
    {
        UIEvent.Instance.onUIGameStartClickNewGame += OnClickNewGameButton;
        UIEvent.Instance.onUIGameStartClickResume += OnClickResumeGameButton;
        
        newGameButton.onClick.AddListener(UIEvent.Instance.OnUIGameStartClickNewGame);
        resumeButton.onClick.AddListener(UIEvent.Instance.OnUIGameStartClickResume);
        leftButton.onClick.AddListener(OnClickLeftButton);
        rightButton.onClick.AddListener(OnClickRightButton);

        leftButtonCanvasGroup = leftButton.GetComponent<CanvasGroup>();
        rightButtonCanvasGroup = rightButton.GetComponent<CanvasGroup>();
        resumeButtonCanvasGroup = resumeButton.GetComponent<CanvasGroup>();

        //hasFade = false;
        
        if (currentLevelIndex == 0)
        {
            //leftButton.gameObject.SetActive(false);
            leftButtonCanvasGroup.alpha = 0;
        }

        
        LevelManager.Instance.SwitchLevel(currentLevelIndex);
    }

    public override void OnEnter()
    {
        base.OnEnter();
        Debug.Log("Enter Game Start");
        LevelManager.Instance.SwitchLevel(currentLevelIndex);
    }


    public void ResumeButtonSwitch(bool isOn)
    {
        if (isOn)
        {
            resumeButton.gameObject.SetActive(true);
            ResumeButtonEffectControl(1,1,true);
        }
        else
        {
            resumeButton.gameObject.SetActive(false);
            ResumeButtonEffectControl(0, 1,false);
        }
    }

    private void OnClickNewGameButton()
    {
        GameManager.Instance.NewGame();
        UIManager.Instance.CloseUI(this);
        UIManager.Instance.OpenUI(UIManager.Instance.GetUI("HUD"));
    }
    private void OnClickResumeGameButton()
    {
        GameManager.Instance.LoadGame();
        UIManager.Instance.CloseUI(this);
        UIManager.Instance.OpenUI(UIManager.Instance.GetUI("HUD"));
    }

    private void ResumeButtonEffectControl(float endValue,float duration,bool isOn)
    {
        resumeButton.transform.localPosition = new Vector3(newGameButton.transform.localPosition.x, newGameButton.transform.localPosition.y-100);
        resumeButtonCanvasGroup.DOKill();
        resumeButton.DOKill();
        if(isOn)
        {
            resumeButtonCanvasGroup.blocksRaycasts = true;
            resumeButton.transform.DOLocalMoveY(resumeButton.transform.localPosition.y-35,duration/2).SetEase(Ease.Linear);
            DOTween.To(() => resumeButtonCanvasGroup.alpha, x => resumeButtonCanvasGroup.alpha = x, endValue, duration);
        }
        else
        {
            resumeButtonCanvasGroup.blocksRaycasts = false;
            resumeButtonCanvasGroup.alpha = 0;
        }
        
    }

    IEnumerator ResumeButtonEffect(float duration)
    {
        if(resumeButtonCanvasGroup.alpha >=1 ) yield break;
        resumeButtonCanvasGroup.alpha = resumeButtonCanvasGroup.alpha + .1f;
        yield return new WaitForSeconds(.1f);
        yield return ResumeButtonEffect(duration-.1f);
    }

    private void rightButtonEffectControl(float endValue,float duration)
    {
        rightButtonCanvasGroup.DOKill();
        if(endValue >0 ) rightButtonCanvasGroup.blocksRaycasts = true;
        else rightButtonCanvasGroup.blocksRaycasts = false;
        DOTween.To(() => rightButtonCanvasGroup.alpha, x => rightButtonCanvasGroup.alpha = x, endValue, duration);
    }

    private void leftButtonEffectControl(float endValue, float duration)
    {
        leftButtonCanvasGroup.DOKill();
        if (endValue > 0) leftButtonCanvasGroup.blocksRaycasts = true;
        else leftButtonCanvasGroup.blocksRaycasts = false;
        DOTween.To(() => leftButtonCanvasGroup.alpha, x => leftButtonCanvasGroup.alpha = x, endValue, duration);
    }

    private void OnClickLeftButton()
    {
        if (currentLevelIndex == 0) return;

        rightButtonEffectControl(1,1);
        
        currentLevelIndex--;
        
        levelSwitchMask.DOKill();
        levelSwitchMask.DOLocalMoveX(currentLevelIndex*-400, .5f);
        
        LevelManager.Instance.currentLevelIndex = currentLevelIndex;
        LevelManager.Instance.SwitchLevel(currentLevelIndex);
        if(currentLevelIndex <= 0)
        {
            leftButtonEffectControl(0,1);
        }
    }

    public void OnClickRightButton()
    {

        leftButtonEffectControl(1,1);
        
        currentLevelIndex++;

        levelSwitchMask.DOKill();
        levelSwitchMask.DOLocalMoveX(currentLevelIndex * -400, .5f);
     
        LevelManager.Instance.currentLevelIndex = currentLevelIndex;
        LevelManager.Instance.SwitchLevel(currentLevelIndex);
        if(currentLevelIndex >= 2)
        {
            rightButtonEffectControl(0,1);
        }
        
    }

    private Vector3 lastPointerPos;
    private Vector3 currentPointerPos;
    
    public void OnSwitchLevelAreaDragStart()
    {
        SetSwitchLevelAreaSize(1080, 2340);
        lastPointerPos = Input.mousePosition;
    }

    public void OnSwitchLevelAreaDrag()
    {
        if (levelSwitchMask.anchoredPosition.x > -950 && levelSwitchMask.anchoredPosition.x < 150)
        {
            currentPointerPos = Input.mousePosition;
            Vector3 pointerDelta = currentPointerPos - lastPointerPos;
            Vector3 newPos = new Vector3(levelSwitchMask.anchoredPosition.x + pointerDelta.x,0);
            levelSwitchMask.anchoredPosition = newPos;
            lastPointerPos = currentPointerPos;
        }
    }

    public void OnSwitchLevelAreaDragEnd()
    {
        SetSwitchLevelAreaSize(470, 100);
        currentLevelIndex = Mathf.RoundToInt(levelSwitchMask.anchoredPosition.x/400);
        int index = currentLevelIndex;
        currentLevelIndex = Mathf.Abs(currentLevelIndex);
        Debug.LogFormat("OnSwitchLevelAreaDragEnd   levelSwitchMaskPos : {0} , currentLevelIndex : {1}", levelSwitchMask.anchoredPosition, currentLevelIndex);
        
        LevelManager.Instance.SwitchLevel(currentLevelIndex);
        if (currentLevelIndex >= 2)
        {
            rightButtonEffectControl(0,1);
        }else if (currentLevelIndex <= 0)
        {
            leftButtonEffectControl(0,1);
        }
        else
        {
            leftButtonEffectControl(1,1);
            rightButtonEffectControl(1,1);
        }
        
        
        levelSwitchMask.DOLocalMoveX(index * 400, .5f);
    }

    public void SetSwitchLevelAreaSize(float x,float y)
    {
        levelSwitchMask.sizeDelta = new Vector2(x,y);
    }
    
}
